
from zoom import zoomer
from common.models.groups import Group
from common.models.user import Users
import dzentity, dzpage, dzutil

#------------------------------------------------------------------------
class ListView(dzentity.Entity):
    def __init__(self):
        data = zoomer.db("select * from dz_groups order by name")
        groups_data = zoomer.db("select * from dz_groups")
        subgroups = zoomer.db("select * from dz_subgroups")
        members = zoomer.db("select * from dz_members")
        users_data = zoomer.db('select * from dz_users')

        groups = dict((i.groupid,dzutil.link_to(i.name,action='show',key=i.groupid)) for i in groups_data)
        users  = dict((i.userid,dzutil.link_to(i.loginid,app='users',con='user',action='show',key=i.userid)) for i in users_data)

        self.labels = ['Name','Type','Users','Members','Member of']
        self.items = [(i.groupid,i.name,i.type, \
            ' '.join([users.get(j.userid,'(%s?)'%j.userid) for j in members if j.groupid==i.groupid]), \
            ' '.join([groups.get(j.subgroupid,'?') for j in subgroups if i.groupid==j.groupid]), \
            ' '.join([groups.get(j.groupid,'?') for j in subgroups if i.groupid==j.subgroupid]), \
            ) \
            for i in data]

    def get_LIST(self):
        return dzutil.browse(self.items,labels=self.labels,con='group',on_click='show')

    def get_COUNT(self):
        return len(self.items)

    def render(self):
        page = dzpage.Page(dzutil.render('views/groups.txt',self))
        page.actions = [dzutil.link_to('Add Group',action='add',con='user')]
        return page.render()

class ShowView(dzentity.Entity):
    def __init__(self,group):
        dzentity.Entity.__init__(self)
        self.group = group
        users_data = zoomer.db('select userid,loginid from dz_users')
        groups_data = zoomer.db('select groupid, name from dz_groups')
        self.user_lookup  = dict((i.userid,dzutil.link_to(i.loginid,app='users',con='user',action='show',key=i.userid)) for i in users_data)
        self.group_lookup = dict((i.groupid,dzutil.link_to(i.name,app='groups',con='group',action='show',key=i.groupid)) for i in groups_data)

    def get_NAME(self):
        return self.group.NAME

    def get_TYPE(self):
        return {'U':'User Group','A':'Application'}.get(self.group.TYPE,'Unknown(%s)'%self.group.TYPE)

    def get_DESCR(self):
        return self.group.DESCR

    def get_GROUPID(self):
        return self.group.GROUPID

    def get_SUBGROUP_LIST(self):
        items = [(item,self.group_lookup.get(item,'(%s?)'%item),) for item in self.group.SUBGROUPS]
        return dzutil.browse(items,on_remove='remove_subgroup',groupid=self.group.GROUPID)

    def get_SUPERGROUP_LIST(self):
        items = [(item,self.group_lookup.get(item,'(%s?)'%item),) for item in self.group.SUPERGROUPS]
        return dzutil.browse(items,on_remove='remove_supergroup',groupid=self.group.GROUPID)

    def get_USER_LIST(self):
        items = [(item,self.user_lookup.get(item,'(%s?)'%item),) for item in self.group.MEMBERS]
        return dzutil.browse(items,on_remove='remove_user',groupid=self.group.GROUPID)

    def get_MEMBERS(self):
        if self.group.TYPE == 'U':
            return dzutil.render('views/group_show_add_user.txt',self) + dzutil.render('views/group_show_add_subgroup.txt',self) + dzutil.render('views/group_show_add_supergroup.txt',self)
        else:
            return dzutil.render('views/group_show_add_subgroup.txt',self)

    def render(self):
        page = dzpage.Page(dzutil.render('views/group_show.txt',self))
        page.actions = [
            dzutil.link_to('List',action='index'),
            dzutil.link_to('Edit',action='edit',key=self.group.GROUPID),
            dzutil.link_to('Delete',action='confirm_delete',key=self.group.GROUPID),
        ]
        return page.render()

class DeleteView(dzentity.Entity):
    def __init__(self,group):
        self.group = group

    def render(self):
        page = dzpage.Page(dzutil.render('views/group_delete.txt',form=1,con='group',action='delete',data=self.group))
        return page.render()

class EditView(ShowView):
    def get_TYPE(self):
        return self.group.TYPE

    def render(self):
        page = dzpage.Page(dzutil.render('views/group_edit.txt',form=1,con='group',action='update',data=self))
        page.actions = [dzutil.link_to('List',action='index'),]
        return page.render()

class AddView:
    def __init__(self,group=Group()):
        self.group = group

    def render(self):
        page = dzpage.Page()
        page.content = dzutil.render('views/group_edit.txt',form=1,con='group',action='insert',data=self.group)
        return page.render()


class ChangePasswordView:
    def __init__(self,user):
        self.user = user

    def render(self):
        page = dzpage.Page()
        self.user.NEW_PASSWORD = ''
        self.user.CONFIRM = ''
        page.content = dzutil.render('views/user_change_password.txt',form=1,data=self.user,action='update_password')
        return page.render()
        
        